<?php

namespace app\interfaces\model;

use app\interfaces\model\MemberBrokerage as MemberBrokerageModel;
use app\interfaces\model\ShopHome as ShopHomeModel;
use app\interfaces\model\ShopType as ShopTypeModel;
use app\interfaces\model\Member as MemberModel;
use think\Model;
use think\Request;

class MerchantShop extends Model
{
    // 分类名称
    protected function getTypeNameAttr($value, $data)
    {
        return ShopTypeModel::where(['id'=>$data['type_id']])->value('title');
    }

    // 设置主表名
    protected $table = 'sun_shop';

    // 模型事件
    protected static function init()
    {

        // 新增前
        self::event('before_insert', function ($data) {
            $data->member_id = Request::instance()->param('shop_id');
            // 刘恒修改status值  // 创建店铺成功 但未认证
            $data->status = '4';
            // $data->status = '1';
            $data->logo = uploadImg('logo', 'shop');
            // $data->picUrl = uploadImg('picUrl', 'shop');
            // $data->company_img = uploadImg('company_img', 'shop');
            $data->create_time = time();
        });

        // 新增后
        self::event('after_insert', function ($data) {

            // 创建会员佣金表
            $model = new MemberBrokerageModel();
            $model->save(['member_id' => $data->member_id]);

            // 创建店铺设置表
            $shop_home_model = new ShopHomeModel();
            $shop_home_model->save(['member_id' => $data->member_id]);

            // 更新店铺状态
            $member_model = new MemberModel();
            $member_model->save(['shop_status' => '1'], ['id' => $data->member_id]);

        });

        // 更新前
        // self::event('before_update', function ($data) {
        //     $data->picUrl = uploadImg('picUrl', 'shop');
        //     $data->company_img = uploadImg('company_img', 'shop');
        // });
    }
}
